
import java.util.LinkedList;

import hibernate.hibernateInterface.RoomSystemHibernateInterface;
import hibernate.roomSystem.Room;

import org.hibernate.Session;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.HtmlMacroComponent;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Label;
import org.zkoss.zul.Window;
import org.zkoss.zul.api.Button;
import org.zkoss.zul.api.Checkbox;
import org.zkoss.zul.api.Datebox;
import org.zkoss.zul.api.Intbox;
import org.zkoss.zul.api.Combobox;
import org.zkoss.zul.api.Textbox;

import utilities.event.CSListener;
import utilities.utilities_components.PlainList;

public class ClientSearchController extends Window{
	public void showPlainList()
	{
		PlainList pList = new PlainList();
		pList.setId("CLIENT_RESULT_LIST");
		String[] header = {"姓名","房号","房型","性别","出生年月","证件类型","证件编码","来自","登记时间","团队","备注"};
		String[] footer = {"合计","0","人次"};
		int[] para1 = {1,1,1},para2 = {0,2,3},para3 = {0,2},para4 = {0,1,2,3,4,8,9};
		pList.setHead(header);
		pList.setFooter(footer, para1,para2,para3);
		pList.setSortable(para4);
		pList.setParent((Component)this);
		this.addEventListener("onNotify", new CSListener());
	}
	
	public void ageBoxCheck()throws Exception
	{
		Intbox box1 = (Intbox)getFellow("CS_LOWER_AGE_BOX");
		Intbox box2 = (Intbox)getFellow("CS_HIGHER_AGE_BOX");
		if (box1.getValue() != null &&
			box2.getValue() != null &&
			box1.getValue() >= box2.getValue())
		{
			box2.setValue(0);
			throw new WrongValueException(box2,"年龄上限应大于下限");
		}
	}
	
	public void dateBoxCheck()throws Exception
	{
		Datebox box1 = (Datebox)getFellow("CS_TIME_FROM_BOX");
		Datebox box2 = (Datebox)getFellow("CS_TIME_TO_BOX");
		if (box1.getValue() != null && 
			box2.getValue() != null &&
			box1.getValue().compareTo(box2.getValue()) >= 0)
		{
			box2.setValue(null);
			throw new WrongValueException(box2,"起始日期应先于截止日期");
		}
	}
	
	public void fillCombobox()
	{
		Combobox searchRes = (Combobox)getFirstChild().getFellow("CS_SEARCH_RESTRICTION_BOX");
		Combobox roomRes = (Combobox)getFirstChild().getFellow("CS_ROOM_RESTRICTION_BOX");
		if (searchRes.getSelectedIndex() == 1)
		{
			roomRes.setDisabled(false);
			String [] roomType = {"单人间","双人间","标准间","套间","三人间","四人间","总统套房"};
			int i = 0;
			int itemCount = roomRes.getItemCount();
			for (;i < itemCount;i++)
			{
				roomRes.removeItemAtApi(0);
			}
			for (i = 0;i < roomType.length;i++)
			{
				roomRes.appendItemApi(roomType[i]);
			}
		}
		else if (searchRes.getSelectedIndex() ==2)
		{
			roomRes.setSelectedItemApi(null);
			roomRes.setDisabled(true);
		}
		else
		{
			roomRes.setDisabled(false);
			this.getPage().setAttribute("roomRes", roomRes);
			this.setAttribute("EVENT_TYPE", "FILL");
			Events.sendEvent(new Event("onNotify",this));
		}
	}
	
	public void onSearch()
	{
		this.getPage().setAttribute("list", (PlainList)getFellow("CLIENT_RESULT_LIST"));
		this.getPage().setAttribute("name", ((Textbox)getFirstChild().getFellow("CS_NAME_BOX")).getValue());
		this.getPage().setAttribute("ageChecked", ((Checkbox)getFirstChild().getFellow("CS_AGE_CHECK")).isChecked());
		this.getPage().setAttribute("low_age", ((Intbox)getFirstChild().getFellow("CS_LOWER_AGE_BOX")).getValue());
		this.getPage().setAttribute("high_age", ((Intbox)getFirstChild().getFellow("CS_HIGHER_AGE_BOX")).getValue());
		this.getPage().setAttribute("timeFrom", ((Datebox)getFirstChild().getFellow("CS_TIME_FROM_BOX")).getValue());
		this.getPage().setAttribute("timeTo", ((Datebox)getFirstChild().getFellow("CS_TIME_TO_BOX")).getValue());
		this.getPage().setAttribute("sex", 2);
		if (((Combobox)getFirstChild().getFellow("CS_SEX_BOX")).getSelectedIndex() == 0)
			this.getPage().setAttribute("sex", 0);
		else if (((Combobox)getFirstChild().getFellow("CS_SEX_BOX")).getSelectedIndex() == 1)
			this.getPage().setAttribute("sex", 1);
		this.getPage().setAttribute("searchRes", ((Combobox)getFirstChild().getFellow("CS_SEARCH_RESTRICTION_BOX")).getSelectedItemApi());

		this.getPage().setAttribute("roomRes", ((Combobox)getFirstChild().getFellow("CS_ROOM_RESTRICTION_BOX")).getSelectedItemApi());
		this.setAttribute("EVENT_TYPE", "SEARCH");
		Events.sendEvent(new Event("onNotify",this));
	}
}
